/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import com.elcom.emocbc.dbmapping.tables.pojos.SmscbMessageResult;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author GiangLT
 */
public class GetMessageInfoResponseParser {

    public static SmscbMessageResult domParse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException, ParseException {
        DocumentBuilder dBuilder = null;
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dBuilder = dbFactory.newDocumentBuilder();

            Document doc = dBuilder.parse(inputStream);
            doc.getDocumentElement().normalize();

            String state = doc.getElementsByTagName("state").item(0).getTextContent();
            String endTime = doc.getElementsByTagName("endTime").item(0).getTextContent();
            String startTime = doc.getElementsByTagName("startTime").item(0).getTextContent();

            SmscbMessageResult result = new SmscbMessageResult();
            final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.S";
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            result.setBroadcastState(state);
            result.setEndTime(new Timestamp(df.parse(endTime).getTime()));
            result.setStartTime(new Timestamp(df.parse(startTime).getTime()));

            return result;
        } finally {
            if (dBuilder != null) {
                dBuilder.reset();
            }
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static SmscbMessageResult parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            GetMessageInfoResponseHandler defaultHandler = new GetMessageInfoResponseHandler();
            saxParser.parse(inputStream, defaultHandler);
            return defaultHandler.getSmscbMessageResult();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static SmscbMessageResult parse(String xml) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        GetMessageInfoResponseHandler defaultHandler = new GetMessageInfoResponseHandler();
        InputSource inputSource = new InputSource(new StringReader(xml));
        saxParser.parse(inputSource, defaultHandler);
        return defaultHandler.getSmscbMessageResult();
    }
}
